function update() {
	//game.p.update();
	updateArrowKeys();
	//updateKeys();
	//updateExercises();
	updateCamera();
	if (game.editor) {
		updateEditor();
	}
}

function updateCamera() {
	game.ctx.setTransform(1, 0, 0, 1, 0, 0);
	//camera.x = (-game.p.x - game.grid / 2) * camera.scale + game.canvas.width/2;
	//camera.y = (-game.p.y - game.grid / 2) * camera.scale + game.canvas.height/2;
	game.ctx.translate(
		camera.x,
		camera.y);
	game.ctx.scale(camera.scale, camera.scale);
}

function draw() {
	//game.ctx.drawImage(content.images["TilesSprite2"], 0, 0);
	drawLevel();
	if (game.editor) {
		drawEditor();
	}
	//game.p.draw();
	drawHud();
}

function drawLevel() {
	var mapLengthX = content.level.map.length;
	var mapLengthY = content.level.map[0].length;
	for (i = 0; i < mapLengthX; i++) {
		for (j = 0; j < mapLengthY; j++) {
			var t = content.level.map[i][j];
			var eindex = parseInt(t.substr(0, 2));
			var h = parseInt(t.substr(2, 2));
			
	    	drawIsoImage(content.images["TilesSprite1"], 
	    		eindex-1, 0, 
	    		game.grid*i-game.grid*j, 
	    		game.grid/2*(i+j) - (h-32)*4,
	    		1,
	    		1
	    		);
	    	if (i < mapLengthX-1) {
	    		var h2 = parseInt(content.level.map[i+1][j].substr(2, 2));
	    		if (h < h2) {
		    	drawIsoImage(content.images["MaskSprite1"], 
		    		1, 2, 
		    		game.grid*i-game.grid*j, 
		    		game.grid/2*(i+j) - (h-32)*4,
		    		0.5,
		    		Math.min(h2 - h, 8)/8
		    		);
	    		}
	    	}
	    	if (j < mapLengthY-1) {
	    		var h2 = parseInt(content.level.map[i][j+1].substr(2, 2));
	    		if (h < h2) {
		    	drawIsoImage(content.images["MaskSprite1"], 
		    		2, 2, 
		    		game.grid*i-game.grid*j, 
		    		game.grid/2*(i+j) - (h-32)*4,
		    		0.5,
		    		Math.min(h2 - h, 8)/8
		    		);
	    		}
	    	}
	    	
	    	//editor
	    	if (game.editor) {
	    		mouseOn(i, j, h);
	    		x = editor.cursor.lastx;
			    y = editor.cursor.lasty;
	    		if (i == x && j == y) {
			    	drawIsoImage(content.images["MaskSprite1"], 
			    		0, 0, 
			    		game.grid*x-game.grid*y, 
			    		game.grid/2*(x+y) - (h-32)*4,
			    		0.5,
			    		1
			    		);
	    		}
	    		
	    	}
    	}
    }
}

function drawIsoImage(spriteSheet, sx, sy, x, y, height, alpha) {
	try {  
	if (alpha < 1) {
		game.ctx.save();
		game.ctx.globalAlpha = alpha
	}
	game.ctx.drawImage(spriteSheet,
		sx * 96, sy * 96 * height, 
		96, 96 * height, 
    	x, y, 96, 96 * height);
    } catch(e) {alert(e)}
    if (alpha < 1) { game.ctx.restore() }
}

function drawHud() {
	game.ctx.save();
	game.ctx.setTransform(1, 0, 0, 1, 0, 0);
	//drawExercises();
	
	//FPS
	game.ctx.fillStyle = "#FFF";
	game.ctx.font = "bold 12px sans-serif";
  	game.ctx.fillText("fps: " + Math.min(fps.curFPS,fps.FPSlimit) + "/" + fps.FPSlimit, 10, 20);
	game.ctx.restore();
}